package com.tmxk.api.days01.days01_wmh.list;

import java.util.ArrayList;

/**
 * @Description动态数组集合
 * @Author:wmh
 * @CreateTime:2022-11-28 10:42
 * @Version: 1.0
 */
public class ArrayListDemo1 {
    public static void main(String[] args) {

        //需求1：是使用ArrayList集合对象 添加5个Dog对象
        //数组存储
        Dog[] dogs = new Dog[5];
        dogs[0] = new Dog("小花", 2);
        dogs[1] = new Dog("小白", 3);
        dogs[2] = new Dog("小黑", 1);
        dogs[3] = new Dog("小灰", 5);
        dogs[4] = new Dog("小青", 6);


        //集合存储
        ArrayList arrayList = new ArrayList<>();  //<>泛型约束
        Dog dog1 = new Dog("小花", 2);
        /**
         Dog  dog1  = new Dog("小花",2);
         Dog  dog2  = new Dog("小白",3);
         Dog  dog3  = new Dog("小黑",1);
         Dog  dog4  = new Dog("小灰",5);
         Dog  dog5  = new Dog("小青",6);
         **/
        arrayList.add(dog1);
        arrayList.add(new Dog("小白", 3));
        arrayList.add(new Dog("小黑", 1));
        arrayList.add(new Dog("小灰", 5));
        arrayList.add(new Dog("小青", 6));
        //需求2：获取第三个下标的狗狗
        Dog dog = (Dog) arrayList.get(2);
        System.out.println("第三个下标元素的狗狗：" + dog);
        //需求3：查找是否存在汪汪这个集合
        //定义一个标识符，用来标识定义的元素是否存在，默认false不存在。
        boolean flag = true;    //有问题？
        for (Object obj : arrayList) {
            Dog d = (Dog) obj;  //类型强转
            String str = "小白".equals(d.getName()) == true ? "包含在该集合元素中 " : "不包含在该集合元素中";
            if ("小白".equals(d.getName())) {
                flag = true;
                System.out.println("存在小白");
            } else {
                System.out.println("不存在小白");
            }
        }

        //需求4：删除小白

        System.out.println("集合元素遍历方式二:  foreach");
        //是否成功删除该集合元素，默认定义false 未删除成功
        boolean isRemove = false;
        for (int i = 0; i < arrayList.size(); i++) {
            Dog d = (Dog) arrayList.get(i);
            if ("小白".equals(d.getName())) {
                isRemove = arrayList.remove(d);
            }
        }
        System.out.println(isRemove == true ? "删除小白成功" : "删除花花失败");

        //需求5：遍历循环该元素
        System.out.println("循环遍历该元素：");
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
    }
}


